﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Giaiphongmatbang.DataModel;

namespace Giaiphongmatbang.BLL
{
    public class HouseTypesRepository
    {
        private QLGPMBEntities data = new QLGPMBEntities();

        public IEnumerable<HouseType> GetAll(int pageSize, int pageNumber, ref int totalRow)
        {
            var results = from p in data.HouseTypes
                          select p;
            totalRow = results.Count();
            return results.OrderBy(p=>p.Type).Skip((pageNumber - 1) * pageSize).Take(pageSize);
        }

        public IEnumerable<HouseType> GetByParentID(int parentID)
        {
            return data.HouseTypes.Where(p => p.ParentID == parentID);
        }

        public IEnumerable<HouseType> GetParentHouseType()
        {
            return data.HouseTypes.Where(p => p.ParentID == 0);
        }

        public HouseType GetByID(int ID)
        {
            var result = from p in data.HouseTypes
                         where p.HouseID == ID
                         select p;
            return result.FirstOrDefault();
        }

        public IEnumerable<HouseType> GetByCMND(string cmnd)
        {
            return data.HouseTypes.Where(p => p.InventoryHouses.Any(q => q.CMND == cmnd));
        }

        public IEnumerable<HouseType> GetByProjectID(int ID)
        {
            return data.HouseTypes.Where(p => p.InventoryHouses.Any(q => q.ProjectID == ID));
        }

        public bool HasChild(int ID)
        {
            return data.HouseTypes.Any(p => p.ParentID == ID);
        }
        public void Insert(HouseType ht)
        {
            data.AddToHouseTypes(ht);
        }

        public void DeleteByID(int id)
        {
            var obj = GetByID(id);
            if (obj != null)
            {
                data.DeleteObject(obj);
            }
        }

        public void Save()
        {
            data.SaveChanges();
        }

    }
}